﻿using EntityFrameworkCore1.Migrations;
using System.Linq;

namespace EntityFrameworkCore1
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            //ctx=逻辑上的数据库
            using (MyDbContext ctx = new MyDbContext())
            {
                /*Dog d = new Dog();
                d.Name = "test";
                //把d对象加入Dogs这个逻辑上的表里面
                ctx.Dogs.Add(d);

                Dog d1 = new Dog();
                d1.Name = "test1";
                ctx.Dogs.Add(d1);

                Book book = new Book();
                book.Title = ".NET";
                book.AuthorName = "zuoyi";
                book.Price = 100;
                book.PubTime = DateTime.Now;
                ctx.Books.Add(book);*/

                /*Book b = new Book { AuthorName = "杨中科", Title = "零基础趣学C语言", PubTime = new DateTime(2019, 3, 1), Price = 59.8 };
                Book b1 = new Book { AuthorName = "Robert Sedgewick", Title = "算法（第四版）", PubTime = new DateTime(2012, 10, 1), Price = 99 };
                Book b2 = new Book { AuthorName = "吴军", Title = "数学之美", PubTime = new DateTime(2020, 5, 1), Price = 69 };
                Book b3 = new Book { AuthorName = "杨中科", Title = "程序员的SQL经典", PubTime = new DateTime(2008, 9, 1), Price = 52 };
                Book b4 = new Book { AuthorName = "吴军", Title = "文明之光", PubTime = new DateTime(2017, 3, 1), Price = 246 };

                ctx.Books.Add(b);
                ctx.Books.Add(b1);
                ctx.Books.Add(b2);
                ctx.Books.Add(b3);
                ctx.Books.Add(b4);*/

                /*Update-database
                ctx.SaveChanges();
                await ctx.SaveChangesAsync();*/

                /*await Console.Out.WriteLineAsync("大于80，按价格升序查询");
                IQueryable<Book> books = ctx.Books.Where(e => e.Price > 80).OrderBy(e => e.Price);
                foreach (Book book in books)
                {
                    await Console.Out.WriteLineAsync(book.Title + "," + book.Price);
                }

                await Console.Out.WriteLineAsync("查询某个记录");
                var book1 = ctx.Books.Single(e => e.Title == "文明之光");
                await Console.Out.WriteLineAsync(book1.AuthorName);

                await Console.Out.WriteLineAsync("分组查询");
                var books2 = ctx.Books.GroupBy(e => e.AuthorName).Select(g => new { AuthorName = g.Key, BooksCount = g.Count(), MaxPrice = g.Max(b => b.Price) });
                foreach (var book in books2)
                {
                    await Console.Out.WriteLineAsync(($"作者名:{book.AuthorName},著作数量:{book.BooksCount},最贵的价格:{book.MaxPrice}"));
                }

                await Console.Out.WriteLineAsync("修改");
                var book2 = ctx.Books.Single(e => e.Title == "文明之光");
                book2.AuthorName = "test";

                await Console.Out.WriteLineAsync("删除");
                var dog = ctx.Dogs.Single(e => e.Id == 2);
                ctx.Dogs.Remove(dog);*/

                await Console.Out.WriteLineAsync("批量更新");
                IQueryable<Book> books3 = ctx.Books.Where(e => e.Price > 80).OrderBy(e => e.Price);
                foreach (Book book in books3)
                {
                    book.Price = book.Price + 1;
                }
                await ctx.SaveChangesAsync();
            }
        }
    }
}